package com.lzq.badminton.config;

import com.lzq.badminton.quartz.AddBookingJob;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

import java.util.Calendar;
import java.util.Date;

@Configuration
public class QuartzConfig {

    // 配置JobDetail
    @Bean
    public JobDetailFactoryBean addBookingJobDetail() {
        JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
        factoryBean.setJobClass(AddBookingJob.class);
        factoryBean.setName("AddBookingJob");
        factoryBean.setGroup("AddBookingJobGroup");
        factoryBean.setDurability(true);
        factoryBean.setRequestsRecovery(true);
        return factoryBean;
    }

    // 配置Trigger(SimpleTriggerFactoryBean, CronTriggerFactoryBean)
    @Bean
    public SimpleTriggerFactoryBean addBookingTrigger(JobDetail alphaJobDetail) {
        SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
        factoryBean.setJobDetail(alphaJobDetail);
        factoryBean.setName("AddBookingTrigger");
        factoryBean.setGroup("AddBookingTriggerGroup");
        factoryBean.setRepeatInterval(1000*60*60*24);//五分钟
        factoryBean.setStartTime(getStartTime());
        factoryBean.setJobDataMap(new JobDataMap());
        return factoryBean;
    }

    // 设置开始时间为当前时间加上20个小时
    private Date getStartTime() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE, 1200); // 开始时间为当前时间加20个小时
        return cal.getTime();
    }


}
